package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-04 15:42
 **/
@Api(tags = "商品SKU接口")
@RestController
@RequestMapping("admin/product")
public class SkuManageController {


    @Autowired
    private ManageService manageService;

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        manageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }



    /**
     * 更新updateSkuInfo
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuInfo skuInfo) {

        // 调用service方法
        manageService.updateSkuInfo(skuInfo);

        return Result.ok();
    }


    /**
     * sku分页列表
     *
     * @param page
     * @param limit
     * @param skuInfo
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SkuInfo skuInfo) {
        //  构建一个Page对象
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        //  调用服务层方法！
        IPage iPage = this.manageService.getSkuInfoPage(skuInfoPage, skuInfo);
        return Result.ok(iPage);
    }

    //http://localhost/admin/product/getSkuInfo/23

    /**
     * 根据skuId  获取SkuInfo
     *
     * @param skuId
     * @return
     */
    @GetMapping("getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId) {

        SkuInfo skuInfo = manageService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }


    //根据spuId 获取spuImage 集合
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        // 服务层调用
        List<SpuImage> spuImageList = manageService.getspuImageList(spuId);
        // 返回数据
        return Result.ok(spuImageList);
    }

    ///admin/product/saveSkuInfo

    /**
     * 保存SkuInfo
     * 商品管理
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        // 服务层调用
        manageService.saveSkuInfo(skuInfo);
        // 返回数据
        return Result.ok();
    }

    /**
     * 根据spuId 查询销售属性
     * http://localhost/admin/product/spuSaleAttrList/26
     *
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {

        // 调用service方法
        List<SpuSaleAttr> list = manageService.getSpuSaleAttrList(spuId);
        // 返回数据
        return Result.ok(list);
    }

}
